<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>保存订单 | 美多商城</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Order/OrderSuccess.html" />
    
    
    <link rel="prev" href="../C05-Order/PlaceOrder.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.3"
        data-chapter-title="保存订单"
        data-filepath="C05-Order/SaveOrder.md"
        data-basepath=".."
        data-revision="Sat Aug 11 2018 20:09:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        美多商城
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Prepare/index.html">
            
                
                    <a href="../C01-Prepare/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目准备
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Prepare/BusinessModel.html">
            
                
                    <a href="../C01-Prepare/BusinessModel.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        商业模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Prepare/DevelopmentProcess.html">
            
                
                    <a href="../C01-Prepare/DevelopmentProcess.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        开发流程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Prepare/ProductAnalysis.html">
            
                
                    <a href="../C01-Prepare/ProductAnalysis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        需求分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-Prepare/ProjectStructure.html">
            
                
                    <a href="../C01-Prepare/ProjectStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        项目架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-Prepare/StartProject.html">
            
                
                    <a href="../C01-Prepare/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-Prepare/Config.html">
            
                
                    <a href="../C01-Prepare/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Users/index.html">
            
                
                    <a href="../C02-Users/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        用户部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Users/UserModel.html">
            
                
                    <a href="../C02-Users/UserModel.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        用户模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Users/RegisterAnalysis.html">
            
                
                    <a href="../C02-Users/RegisterAnalysis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        注册业务接口分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Users/ImageCode.html">
            
                
                    <a href="../C02-Users/ImageCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        图片验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Users/SMSCode.html">
            
                
                    <a href="../C02-Users/SMSCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        短信验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Users/CORS.html">
            
                
                    <a href="../C02-Users/CORS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        跨域CORS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="C02-Users/CelerySendSMSCode.html">
            
                
                    <a href="../C02-Users/CelerySendSMSCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        使用Celery发送短信
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Users/AccountExistingCheck.html">
            
                
                    <a href="../C02-Users/AccountExistingCheck.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        判断帐号是否存在
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Users/Register.html">
            
                
                    <a href="../C02-Users/Register.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        注册
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="C02-Users/JWT.html">
            
                
                    <a href="../C02-Users/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        JWT
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="C02-Users/DRF-JWT.html">
            
                
                    <a href="../C02-Users/DRF-JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.</b>
                        
                        Django REST framework JWT
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="C02-Users/Login.html">
            
                
                    <a href="../C02-Users/Login.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.11.</b>
                        
                        登录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="C02-Users/QQLogin/index.html">
            
                
                    <a href="../C02-Users/QQLogin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.12.</b>
                        
                        QQ登录
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.12.1" data-path="C02-Users/QQLogin/Step1.html">
            
                
                    <a href="../C02-Users/QQLogin/Step1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.12.1.</b>
                        
                        第一步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.12.2" data-path="C02-Users/QQLogin/Step2.html">
            
                
                    <a href="../C02-Users/QQLogin/Step2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.12.2.</b>
                        
                        第二步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.12.3" data-path="C02-Users/QQLogin/Step3.html">
            
                
                    <a href="../C02-Users/QQLogin/Step3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.12.3.</b>
                        
                        第三步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="C02-Users/UserInfo.html">
            
                
                    <a href="../C02-Users/UserInfo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.13.</b>
                        
                        用户中心个人信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="C02-Users/Email/index.html">
            
                
                    <a href="../C02-Users/Email/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.14.</b>
                        
                        邮件与验证
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.14.1" data-path="C02-Users/Email/DjangoEmail.html">
            
                
                    <a href="../C02-Users/Email/DjangoEmail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.14.1.</b>
                        
                        Django发送邮件的方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.14.2" data-path="C02-Users/Email/SendEmail.html">
            
                
                    <a href="../C02-Users/Email/SendEmail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.14.2.</b>
                        
                        保存邮箱并发送验证邮件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.14.3" data-path="C02-Users/Email/VerifyEmail.html">
            
                
                    <a href="../C02-Users/Email/VerifyEmail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.14.3.</b>
                        
                        验证邮箱链接
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="C02-Users/Address/index.html">
            
                
                    <a href="../C02-Users/Address/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.15.</b>
                        
                        收货地址
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.15.1" data-path="C02-Users/Address/AreaSelect.html">
            
                
                    <a href="../C02-Users/Address/AreaSelect.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.15.1.</b>
                        
                        省市区地址查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.15.2" data-path="C02-Users/Address/Cache.html">
            
                
                    <a href="../C02-Users/Address/Cache.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.15.2.</b>
                        
                        使用缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.15.3" data-path="C02-Users/Address/UserAddressManagement.html">
            
                
                    <a href="../C02-Users/Address/UserAddressManagement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.15.3.</b>
                        
                        用户地址管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.15.4" data-path="C02-Users/Address/UserAddressCode.html">
            
                
                    <a href="../C02-Users/Address/UserAddressCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.15.4.</b>
                        
                        用户地址代码
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Goods/index.html">
            
                
                    <a href="../C03-Goods/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        商品部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Goods/DatabaseDesign.html">
            
                
                    <a href="../C03-Goods/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        数据表设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Goods/FastDFS.html">
            
                
                    <a href="../C03-Goods/FastDFS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        FastDFS分布式文件系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Goods/Docker/index.html">
            
                
                    <a href="../C03-Goods/Docker/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Docker使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="C03-Goods/Docker/Introduce.html">
            
                
                    <a href="../C03-Goods/Docker/Introduce.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        Docker简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="C03-Goods/Docker/InstallAndOperations.html">
            
                
                    <a href="../C03-Goods/Docker/InstallAndOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        安装与操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Goods/FastDFSDocker.html">
            
                
                    <a href="../C03-Goods/FastDFSDocker.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        使用Docker安装FastDFS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="C03-Goods/FastDFSClientAndDjangoFileSystem.html">
            
                
                    <a href="../C03-Goods/FastDFSClientAndDjangoFileSystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        FastDFS客户端与自定义文件存储系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="C03-Goods/CKEditor.html">
            
                
                    <a href="../C03-Goods/CKEditor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        CKEditor富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="C03-Goods/ImportGoodsData.html">
            
                
                    <a href="../C03-Goods/ImportGoodsData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        添加测试数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="C03-Goods/FreezeHTML.html">
            
                
                    <a href="../C03-Goods/FreezeHTML.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        页面静态化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="C03-Goods/DjangoCrontab.html">
            
                
                    <a href="../C03-Goods/DjangoCrontab.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        定时任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="C03-Goods/GenerateIndexHTMLScript.html">
            
                
                    <a href="../C03-Goods/GenerateIndexHTMLScript.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        静态化首页的手动脚本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="C03-Goods/GoodsDetail.html">
            
                
                    <a href="../C03-Goods/GoodsDetail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.11.</b>
                        
                        商品详情页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="C03-Goods/UserBrowseHistory.html">
            
                
                    <a href="../C03-Goods/UserBrowseHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.12.</b>
                        
                        用户浏览历史记录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.13" data-path="C03-Goods/GoodsList.html">
            
                
                    <a href="../C03-Goods/GoodsList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.13.</b>
                        
                        商品列表页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.14" data-path="C03-Goods/GoodsSearch.html">
            
                
                    <a href="../C03-Goods/GoodsSearch.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.14.</b>
                        
                        商品搜索
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-Cart/index.html">
            
                
                    <a href="../C04-Cart/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        购物车部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-Cart/DataStructureDesign.html">
            
                
                    <a href="../C04-Cart/DataStructureDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        购物车数据存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-Cart/Add.html">
            
                
                    <a href="../C04-Cart/Add.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        购物车增加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-Cart/Query.html">
            
                
                    <a href="../C04-Cart/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        购物车查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-Cart/Modify.html">
            
                
                    <a href="../C04-Cart/Modify.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        购物车修改
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-Cart/Delete.html">
            
                
                    <a href="../C04-Cart/Delete.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        购物车删除
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="C04-Cart/SelectAll.html">
            
                
                    <a href="../C04-Cart/SelectAll.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        购物车全选
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="C04-Cart/MergeCart.html">
            
                
                    <a href="../C04-Cart/MergeCart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        登录合并购物车
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Order/index.html">
            
                
                    <a href="../C05-Order/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        订单部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Order/OrderDBModel.html">
            
                
                    <a href="../C05-Order/OrderDBModel.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        订单数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Order/PlaceOrder.html">
            
                
                    <a href="../C05-Order/PlaceOrder.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        订单结算
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.3" data-path="C05-Order/SaveOrder.html">
            
                
                    <a href="../C05-Order/SaveOrder.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        保存订单
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Order/OrderSuccess.html">
            
                
                    <a href="../C05-Order/OrderSuccess.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        下单成功
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Pay/index.html">
            
                
                    <a href="../C06-Pay/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        支付宝支付
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Pay/Alipay.html">
            
                
                    <a href="../C06-Pay/Alipay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        接入支付宝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Pay/Paying.html">
            
                
                    <a href="../C06-Pay/Paying.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        发起支付
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Pay/PaymentResult.html">
            
                
                    <a href="../C06-Pay/PaymentResult.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        保存支付结果
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-Xadmin.html">
            
                
                    <a href="../C07-Xadmin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Xadmin
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-Permission.html">
            
                
                    <a href="../C08-Permission.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        用户权限控制
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >美多商城</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4FDD;&#x5B58;&#x8BA2;&#x5355;">&#x4FDD;&#x5B58;&#x8BA2;&#x5355;</h1>
<h3 id="1-&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;">1. &#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;</h3>
<p><strong>&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</strong> &#xFF1A; POST  /orders/</p>
<p><strong>&#x8BF7;&#x6C42;&#x53C2;&#x6570;</strong>&#xFF1A; JSON  &#x6216; &#x8868;&#x5355;</p>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x662F;&#x5426;&#x5FC5;&#x987B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>address</td>
<td>int</td>
<td>&#x662F;</td>
<td>&#x6536;&#x8D27;&#x5730;&#x5740;id</td>
</tr>
<tr>
<td>pay_method</td>
<td>int</td>
<td>&#x662F;</td>
<td>&#x652F;&#x4ED8;&#x65B9;&#x5F0F;</td>
</tr>
</tbody>
</table>
<p><strong>&#x8FD4;&#x56DE;&#x6570;&#x636E;</strong>&#xFF1A; JSON</p>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x662F;&#x5426;&#x5FC5;&#x987B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>order_id</td>
<td>char</td>
<td>&#x662F;</td>
<td>&#x8BA2;&#x5355;&#x7F16;&#x53F7;</td>
</tr>
</tbody>
</table>
<h3 id="2--&#x540E;&#x7AEF;&#x5B9E;&#x73B0;">2.  &#x540E;&#x7AEF;&#x5B9E;&#x73B0;</h3>
<p>&#x5728;orders/views.py&#x4E2D;&#x521B;&#x5EFA;&#x89C6;&#x56FE;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SaveOrderView</span><span class="hljs-params">(CreateAPIView)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x4FDD;&#x5B58;&#x8BA2;&#x5355;
    &quot;&quot;&quot;</span>
    permission_classes = [IsAuthenticated]
    serializer_class = SaveOrderSerializer
</code></pre>
<p>&#x5728;orders/serializers.py&#x4E2D;&#x521B;&#x5EFA;&#x5E8F;&#x5217;&#x5316;&#x5668;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SaveOrderSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x4E0B;&#x5355;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;
    &quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = OrderInfo
        fields = (<span class="hljs-string">&apos;order_id&apos;</span>, <span class="hljs-string">&apos;address&apos;</span>, <span class="hljs-string">&apos;pay_method&apos;</span>)
        read_only_fields = (<span class="hljs-string">&apos;order_id&apos;</span>,)
        extra_kwargs = {
            <span class="hljs-string">&apos;address&apos;</span>: {
                <span class="hljs-string">&apos;write_only&apos;</span>: <span class="hljs-keyword">True</span>,
                <span class="hljs-string">&apos;required&apos;</span>: <span class="hljs-keyword">True</span>,
            },
            <span class="hljs-string">&apos;pay_method&apos;</span>: {
                <span class="hljs-string">&apos;write_only&apos;</span>: <span class="hljs-keyword">True</span>,
                <span class="hljs-string">&apos;required&apos;</span>: <span class="hljs-keyword">True</span>
            }
        }

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, validated_data)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&quot;&quot;&quot;</span>
        <span class="hljs-keyword">pass</span>
</code></pre>
<h4 id="&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x7684;&#x601D;&#x8DEF;">&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x7684;&#x601D;&#x8DEF;</h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, validated_data)</span>:</span>
    <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x4E0B;&#x5355;&#x7528;&#x6237;</span>

    <span class="hljs-comment"># &#x751F;&#x6210;&#x8BA2;&#x5355;&#x7F16;&#x53F7;</span>

    <span class="hljs-comment"># &#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x57FA;&#x672C;&#x4FE1;&#x606F;&#x6570;&#x636E; OrderInfo</span>

    <span class="hljs-comment"># &#x4ECE;redis&#x4E2D;&#x83B7;&#x53D6;&#x8D2D;&#x7269;&#x8F66;&#x7ED3;&#x7B97;&#x5546;&#x54C1;&#x6570;&#x636E;</span>

    <span class="hljs-comment"># &#x904D;&#x5386;&#x7ED3;&#x7B97;&#x5546;&#x54C1;&#xFF1A;</span>

        <span class="hljs-comment"># &#x5224;&#x65AD;&#x5546;&#x54C1;&#x5E93;&#x5B58;&#x662F;&#x5426;&#x5145;&#x8DB3;</span>

        <span class="hljs-comment"># &#x51CF;&#x5C11;&#x5546;&#x54C1;&#x5E93;&#x5B58;&#xFF0C;&#x589E;&#x52A0;&#x5546;&#x54C1;&#x9500;&#x91CF;</span>

        <span class="hljs-comment"># &#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x5546;&#x54C1;&#x6570;&#x636E;</span>

    <span class="hljs-comment"># &#x5728;redis&#x8D2D;&#x7269;&#x8F66;&#x4E2D;&#x5220;&#x9664;&#x5DF2;&#x8BA1;&#x7B97;&#x5546;&#x54C1;&#x6570;&#x636E;</span>
</code></pre>
<h4 id="&#x6570;&#x636E;&#x5E93;&#x4E8B;&#x52A1;">&#x6570;&#x636E;&#x5E93;&#x4E8B;&#x52A1;</h4>
<p>&#x5728;&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x6570;&#x636E;&#x4E2D;&#xFF0C;&#x6D89;&#x53CA;&#x5230;&#x591A;&#x5F20;&#x8868;&#xFF08;OrderInfo&#x3001;OrderGoods&#x3001;SKU&#xFF09;&#x7684;&#x6570;&#x636E;&#x4FEE;&#x6539;&#xFF0C;&#x5BF9;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x7684;&#x4FEE;&#x6539;&#x5E94;&#x8BE5;&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x4F53;&#x4E8B;&#x52A1;&#xFF0C;&#x5373;&#x8981;&#x4E48;&#x4E00;&#x8D77;&#x6210;&#x529F;&#xFF0C;&#x8981;&#x4E48;&#x4E00;&#x8D77;&#x5931;&#x8D25;&#x3002;</p>
<p>Django&#x4E2D;&#x5BF9;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x4E8B;&#x52A1;&#xFF0C;&#x9ED8;&#x8BA4;&#x6BCF;&#x6267;&#x884C;&#x4E00;&#x53E5;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#xFF0C;&#x4FBF;&#x4F1A;&#x81EA;&#x52A8;&#x63D0;&#x4EA4;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x4E2D;&#x81EA;&#x5DF1;&#x63A7;&#x5236;&#x6570;&#x636E;&#x5E93;&#x4E8B;&#x52A1;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;&#x3002;</p>
<p>&#x5728;Django&#x4E2D;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>django.db.transaction</code>&#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x7684;<code>atomic</code>&#x6765;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#xFF0C;<code>atomic</code>&#x63D0;&#x4F9B;&#x4E24;&#x79CD;&#x7528;&#x6CD5;</p>
<ul>
<li><p>&#x88C5;&#x9970;&#x5668;&#x7528;&#x6CD5;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> transaction

<span class="hljs-decorator">@transaction.atomic</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">viewfunc</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># &#x8FD9;&#x4E9B;&#x4EE3;&#x7801;&#x4F1A;&#x5728;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4E2D;&#x6267;&#x884C;</span>
    ...
</code></pre>
</li>
<li><p>with&#x8BED;&#x53E5;&#x7528;&#x6CD5;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> transaction

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">viewfunc</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># &#x8FD9;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x4E0D;&#x5728;&#x4E8B;&#x52A1;&#x4E2D;&#xFF0C;&#x4F1A;&#x88AB;Django&#x81EA;&#x52A8;&#x63D0;&#x4EA4;</span>
    ...

    <span class="hljs-keyword">with</span> transaction.atomic():
        <span class="hljs-comment"># &#x8FD9;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x4F1A;&#x5728;&#x4E8B;&#x52A1;&#x4E2D;&#x6267;&#x884C;</span>
        ...
</code></pre>
</li>
</ul>
<p>&#x5728;Django&#x4E2D;&#xFF0C;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x4FDD;&#x5B58;&#x70B9;&#x7684;&#x652F;&#x6301;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4E8B;&#x52A1;&#x4E2D;&#x521B;&#x5EFA;&#x4FDD;&#x5B58;&#x70B9;&#x6765;&#x8BB0;&#x5F55;&#x6570;&#x636E;&#x7684;&#x7279;&#x5B9A;&#x72B6;&#x6001;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6062;&#x590D;&#x5230;&#x6570;&#x636E;&#x4FDD;&#x5B58;&#x70B9;&#x7684;&#x72B6;&#x6001;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> transaction

<span class="hljs-comment"># &#x521B;&#x5EFA;&#x4FDD;&#x5B58;&#x70B9;</span>
save_id = transaction.savepoint()  

<span class="hljs-comment"># &#x56DE;&#x6EDA;&#x5230;&#x4FDD;&#x5B58;&#x70B9;</span>
transaction.savepoint_rollback(save_id)
</code></pre>
<h4 id="&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x6570;&#x636E;create&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;">&#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x6570;&#x636E;create&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;</h4>
<pre><code class="lang-python">    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, validated_data)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x4FDD;&#x5B58;&#x8BA2;&#x5355;
        &quot;&quot;&quot;</span>
        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x4E0B;&#x5355;&#x7528;&#x6237;</span>
        user = self.context[<span class="hljs-string">&apos;request&apos;</span>].user

        <span class="hljs-comment"># &#x7EC4;&#x7EC7;&#x8BA2;&#x5355;&#x7F16;&#x53F7; 20170903153611+user.id</span>
        <span class="hljs-comment"># timezone.now() -&gt; datetime</span>
        order_id = timezone.now().strftime(<span class="hljs-string">&apos;%Y%m%d%H%M%S&apos;</span>) + (<span class="hljs-string">&apos;%09d&apos;</span> % user.id)

        address = validated_data[<span class="hljs-string">&apos;address&apos;</span>]
        pay_method = validated_data[<span class="hljs-string">&apos;pay_method&apos;</span>]

        <span class="hljs-comment"># &#x751F;&#x6210;&#x8BA2;&#x5355;</span>
        <span class="hljs-keyword">with</span> transaction.atomic():
            <span class="hljs-comment"># &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4FDD;&#x5B58;&#x70B9;</span>
            save_id = transaction.savepoint()

            <span class="hljs-keyword">try</span>:
                 <span class="hljs-comment"># &#x521B;&#x5EFA;&#x8BA2;&#x5355;&#x4FE1;&#x606F;</span>
                order = OrderInfo.objects.create(
                    order_id=order_id,
                    user=user,
                    address=address,
                    total_count=<span class="hljs-number">0</span>,
                    total_amount=Decimal(<span class="hljs-number">0</span>),
                    freight=Decimal(<span class="hljs-number">10</span>),
                    pay_method=pay_method,
                    status=OrderInfo.ORDER_STATUS_ENUM[<span class="hljs-string">&apos;UNSEND&apos;</span>] <span class="hljs-keyword">if</span> pay_method == OrderInfo.PAY_METHODS_ENUM[<span class="hljs-string">&apos;CASH&apos;</span>] <span class="hljs-keyword">else</span> OrderInfo.ORDER_STATUS_ENUM[<span class="hljs-string">&apos;UNPAID&apos;</span>]
                )
                <span class="hljs-comment"># &#x83B7;&#x53D6;&#x8D2D;&#x7269;&#x8F66;&#x4FE1;&#x606F;</span>
                redis_conn = get_redis_connection(<span class="hljs-string">&quot;cart&quot;</span>)
                redis_cart = redis_conn.hgetall(<span class="hljs-string">&quot;cart_%s&quot;</span> % user.id)
                cart_selected = redis_conn.smembers(<span class="hljs-string">&apos;cart_selected_%s&apos;</span> % user.id)

                <span class="hljs-comment"># &#x5C06;bytes&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4E3A;int&#x7C7B;&#x578B;</span>
                cart = {}
                <span class="hljs-keyword">for</span> sku_id <span class="hljs-keyword">in</span> cart_selected:
                    cart[int(sku_id)] = int(redis_cart[sku_id])

                <span class="hljs-comment"># &#x4E00;&#x6B21;&#x67E5;&#x8BE2;&#x51FA;&#x6240;&#x6709;&#x5546;&#x54C1;&#x6570;&#x636E;</span>
                skus = SKU.objects.filter(id__in=cart.keys())

                <span class="hljs-comment"># &#x5904;&#x7406;&#x8BA2;&#x5355;&#x5546;&#x54C1;</span>
                <span class="hljs-keyword">for</span> sku <span class="hljs-keyword">in</span> skus:
                    sku_count = cart[sku.id]

                    <span class="hljs-comment"># &#x5224;&#x65AD;&#x5E93;&#x5B58;</span>
                    origin_stock = sku.stock  <span class="hljs-comment"># &#x539F;&#x59CB;&#x5E93;&#x5B58;</span>
                    origin_sales = sku.sales  <span class="hljs-comment"># &#x539F;&#x59CB;&#x9500;&#x91CF;</span>

                    <span class="hljs-keyword">if</span> sku_count &gt; origin_stock:
                        transaction.savepoint_rollback(save_id)
                        <span class="hljs-keyword">raise</span> serializers.ValidationError(<span class="hljs-string">&apos;&#x5546;&#x54C1;&#x5E93;&#x5B58;&#x4E0D;&#x8DB3;&apos;</span>)

                    <span class="hljs-comment"># &#x7528;&#x4E8E;&#x6F14;&#x793A;&#x5E76;&#x53D1;&#x4E0B;&#x5355;</span>
                    <span class="hljs-comment"># import time</span>
                    <span class="hljs-comment"># time.sleep(5)</span>

                    <span class="hljs-comment"># &#x51CF;&#x5C11;&#x5E93;&#x5B58;</span>
                    new_stock = origin_stock - sku_count
                    new_sales = origin_sales + sku_count

                    sku.stock = new_stock
                    sku.sales = new_sales
                    sku.save()

                    <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x5546;&#x54C1;&#x7684;SPU &#x9500;&#x91CF;&#x4FE1;&#x606F;</span>
                    sku.goods.sales += sku_count
                    sku.goods.save()

                    <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x8BA2;&#x5355;&#x57FA;&#x672C;&#x4FE1;&#x606F;&#x7684;&#x6570;&#x636E;</span>
                    order.total_count += sku_count  <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x603B;&#x91D1;&#x989D;</span>
                    order.total_amount += (sku.price * sku_count)  <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x603B;&#x989D;</span>

                    <span class="hljs-comment"># &#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x5546;&#x54C1;</span>
                    OrderGoods.objects.create(
                        order=order,
                        sku=sku,
                        count=sku_count,
                        price=sku.price,
                    )

                <span class="hljs-comment"># &#x66F4;&#x65B0;&#x8BA2;&#x5355;&#x7684;&#x91D1;&#x989D;&#x6570;&#x91CF;&#x4FE1;&#x606F;</span>
                order.total_amount += order.freight
                order.save()

            <span class="hljs-keyword">except</span> ValidationError:
                <span class="hljs-keyword">raise</span>
            <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
                logger.error(e)
                transaction.savepoint_rollback(save_id)
                <span class="hljs-keyword">raise</span>

            <span class="hljs-comment"># &#x66F4;&#x65B0;redis&#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x8D2D;&#x7269;&#x8F66;&#x6570;&#x636E;</span>
            pl = redis_conn.pipeline()
            pl.hdel(<span class="hljs-string">&apos;cart_%s&apos;</span> % user.id, *cart_selected)
            pl.srem(<span class="hljs-string">&apos;cart_selected_%s&apos;</span> % user.id, *cart_selected)
            pl.execute()
            <span class="hljs-keyword">return</span> order
</code></pre>
<h3 id="3-&#x5E76;&#x53D1;&#x5904;&#x7406;">3. &#x5E76;&#x53D1;&#x5904;&#x7406;</h3>
<p>&#x5728;&#x591A;&#x4E2A;&#x7528;&#x6237;&#x540C;&#x65F6;&#x53D1;&#x8D77;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5546;&#x54C1;&#x7684;&#x4E0B;&#x5355;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;&#x5148;&#x67E5;&#x8BE2;&#x5546;&#x54C1;&#x5E93;&#x5B58;&#xFF0C;&#x518D;&#x4FEE;&#x6539;&#x5546;&#x54C1;&#x5E93;&#x5B58;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x8D44;&#x6E90;&#x7ADE;&#x4E89;&#x95EE;&#x9898;&#xFF0C;&#x5BFC;&#x81F4;&#x5E93;&#x5B58;&#x7684;&#x6700;&#x7EC8;&#x7ED3;&#x679C;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x3002;</p>
<p><img src="../images/&#x5E76;&#x53D1;&#x4E0B;&#x5355;.png" alt="&#x5E76;&#x53D1;&#x4E0B;&#x5355;"></p>
<h4 id="&#x89E3;&#x51B3;&#x529E;&#x6CD5;&#xFF1A;">&#x89E3;&#x51B3;&#x529E;&#x6CD5;&#xFF1A;</h4>
<ul>
<li><p><strong>&#x60B2;&#x89C2;&#x9501;</strong></p>
<p>&#x5F53;&#x67E5;&#x8BE2;&#x67D0;&#x6761;&#x8BB0;&#x5F55;&#x65F6;&#xFF0C;&#x5373;&#x8BA9;&#x6570;&#x636E;&#x5E93;&#x4E3A;&#x8BE5;&#x8BB0;&#x5F55;&#x52A0;&#x9501;&#xFF0C;&#x9501;&#x4F4F;&#x8BB0;&#x5F55;&#x540E;&#x522B;&#x4EBA;&#x65E0;&#x6CD5;&#x64CD;&#x4F5C;&#xFF0C;&#x4F7F;&#x7528;&#x7C7B;&#x4F3C;&#x5982;&#x4E0B;&#x8BED;&#x6CD5;</p>
<pre><code class="lang-python">select stock <span class="hljs-keyword">from</span> tb_sku where id=<span class="hljs-number">1</span> <span class="hljs-keyword">for</span> update;

SKU.objects.select_for_update().get(id=<span class="hljs-number">1</span>)
</code></pre>
<p>&#x60B2;&#x89C2;&#x9501;&#x7C7B;&#x4F3C;&#x4E8E;&#x6211;&#x4EEC;&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x8D44;&#x6E90;&#x7ADE;&#x4E89;&#x65F6;&#x6DFB;&#x52A0;&#x7684;&#x4E92;&#x65A5;&#x9501;&#xFF0C;&#x5BB9;&#x6613;&#x51FA;&#x73B0;<strong>&#x6B7B;&#x9501;&#x73B0;&#x8C61;</strong>&#xFF0C;&#x91C7;&#x7528;&#x4E0D;&#x591A;&#x3002;</p>
</li>
<li><p><strong>&#x4E50;&#x89C2;&#x9501;</strong></p>
<p>&#x4E50;&#x89C2;&#x9501;&#x5E76;&#x4E0D;&#x662F;&#x771F;&#x5B9E;&#x5B58;&#x5728;&#x7684;&#x9501;&#xFF0C;&#x800C;&#x662F;&#x5728;&#x66F4;&#x65B0;&#x7684;&#x65F6;&#x5019;&#x5224;&#x65AD;&#x6B64;&#x65F6;&#x7684;&#x5E93;&#x5B58;&#x662F;&#x5426;&#x662F;&#x4E4B;&#x524D;&#x67E5;&#x8BE2;&#x51FA;&#x7684;&#x5E93;&#x5B58;&#xFF0C;&#x5982;&#x679C;&#x76F8;&#x540C;&#xFF0C;&#x8868;&#x793A;&#x6CA1;&#x4EBA;&#x4FEE;&#x6539;&#xFF0C;&#x53EF;&#x4EE5;&#x66F4;&#x65B0;&#x5E93;&#x5B58;&#xFF0C;&#x5426;&#x5219;&#x8868;&#x793A;&#x522B;&#x4EBA;&#x62A2;&#x8FC7;&#x8D44;&#x6E90;&#xFF0C;&#x4E0D;&#x518D;&#x6267;&#x884C;&#x5E93;&#x5B58;&#x66F4;&#x65B0;&#x3002;&#x7C7B;&#x4F3C;&#x5982;&#x4E0B;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-python">update tb_sku set stock=<span class="hljs-number">2</span> where id=<span class="hljs-number">1</span> <span class="hljs-keyword">and</span> stock=<span class="hljs-number">7</span>;

SKU.objects.filter(id=<span class="hljs-number">1</span>, stock=<span class="hljs-number">7</span>).update(stock=<span class="hljs-number">2</span>)
</code></pre>
</li>
<li><p><strong>&#x4EFB;&#x52A1;&#x961F;&#x5217;</strong></p>
<p>&#x5C06;&#x4E0B;&#x5355;&#x7684;&#x903B;&#x8F91;&#x653E;&#x5230;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x4E2D;&#xFF08;&#x5982;celery&#xFF09;&#xFF0C;&#x5C06;&#x5E76;&#x884C;&#x8F6C;&#x4E3A;&#x4E32;&#x884C;&#xFF0C;&#x6240;&#x6709;&#x4EBA;&#x6392;&#x961F;&#x4E0B;&#x5355;&#x3002;&#x6BD4;&#x5982;&#x5F00;&#x542F;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x7684;Celery&#xFF0C;&#x4E00;&#x4E2A;&#x8BA2;&#x5355;&#x4E00;&#x4E2A;&#x8BA2;&#x5355;&#x7684;&#x5904;&#x7406;&#x3002;</p>
</li>
</ul>
<h3 id="4-&#x4F7F;&#x7528;&#x4E50;&#x89C2;&#x9501;&#x6539;&#x5199;&#x4E0B;&#x5355;&#x903B;&#x8F91;">4. &#x4F7F;&#x7528;&#x4E50;&#x89C2;&#x9501;&#x6539;&#x5199;&#x4E0B;&#x5355;&#x903B;&#x8F91;</h3>
<pre><code class="lang-python">    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, validated_data)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x4FDD;&#x5B58;&#x8BA2;&#x5355;
        &quot;&quot;&quot;</span>
        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x4E0B;&#x5355;&#x7528;&#x6237;</span>
        user = self.context[<span class="hljs-string">&apos;request&apos;</span>].user

        <span class="hljs-comment"># &#x7EC4;&#x7EC7;&#x8BA2;&#x5355;&#x7F16;&#x53F7; 20170903153611+user.id</span>
        <span class="hljs-comment"># timezone.now() -&gt; datetime</span>
        order_id = timezone.now().strftime(<span class="hljs-string">&apos;%Y%m%d%H%M%S&apos;</span>) + (<span class="hljs-string">&apos;%09d&apos;</span> % user.id)

        address = validated_data[<span class="hljs-string">&apos;address&apos;</span>]
        pay_method = validated_data[<span class="hljs-string">&apos;pay_method&apos;</span>]

        <span class="hljs-comment"># &#x751F;&#x6210;&#x8BA2;&#x5355;</span>
        <span class="hljs-keyword">with</span> transaction.atomic():
            <span class="hljs-comment"># &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4FDD;&#x5B58;&#x70B9;</span>
            save_id = transaction.savepoint()

            <span class="hljs-keyword">try</span>:
                <span class="hljs-comment"># &#x521B;&#x5EFA;&#x8BA2;&#x5355;&#x4FE1;&#x606F;</span>
                order = OrderInfo.objects.create(
                    order_id=order_id,
                    user=user,
                    address=address,
                    total_count=<span class="hljs-number">0</span>,
                    total_amount=Decimal(<span class="hljs-number">0</span>),
                    freight=Decimal(<span class="hljs-number">10</span>),
                    pay_method=pay_method,
                    status=OrderInfo.ORDER_STATUS_ENUM[<span class="hljs-string">&apos;UNSEND&apos;</span>] <span class="hljs-keyword">if</span> pay_method == OrderInfo.PAY_METHODS_ENUM[<span class="hljs-string">&apos;CASH&apos;</span>] <span class="hljs-keyword">else</span> OrderInfo.ORDER_STATUS_ENUM[<span class="hljs-string">&apos;UNPAID&apos;</span>]
                )
                <span class="hljs-comment"># &#x83B7;&#x53D6;&#x8D2D;&#x7269;&#x8F66;&#x4FE1;&#x606F;</span>
                redis_conn = get_redis_connection(<span class="hljs-string">&quot;cart&quot;</span>)
                redis_cart = redis_conn.hgetall(<span class="hljs-string">&quot;cart_%s&quot;</span> % user.id)
                cart_selected = redis_conn.smembers(<span class="hljs-string">&apos;cart_selected_%s&apos;</span> % user.id)

                <span class="hljs-comment"># &#x5C06;bytes&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4E3A;int&#x7C7B;&#x578B;</span>
                cart = {}
                <span class="hljs-keyword">for</span> sku_id <span class="hljs-keyword">in</span> cart_selected:
                    cart[int(sku_id)] = int(redis_cart[sku_id])

                <span class="hljs-comment"># # &#x4E00;&#x6B21;&#x67E5;&#x8BE2;&#x51FA;&#x6240;&#x6709;&#x5546;&#x54C1;&#x6570;&#x636E;</span>
                <span class="hljs-comment"># skus = SKU.objects.filter(id__in=cart.keys())</span>

                <span class="hljs-comment"># &#x5904;&#x7406;&#x8BA2;&#x5355;&#x5546;&#x54C1;</span>
                sku_id_list = cart.keys()
                <span class="hljs-keyword">for</span> sku_id <span class="hljs-keyword">in</span> sku_id_list:
                    <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
                        sku = SKU.objects.get(id=sku_id)

                        sku_count = cart[sku.id]

                        <span class="hljs-comment"># &#x5224;&#x65AD;&#x5E93;&#x5B58;</span>
                        origin_stock = sku.stock  <span class="hljs-comment"># &#x539F;&#x59CB;&#x5E93;&#x5B58;</span>
                        origin_sales = sku.sales  <span class="hljs-comment"># &#x539F;&#x59CB;&#x9500;&#x91CF;</span>

                        <span class="hljs-keyword">if</span> sku_count &gt; origin_stock:
                            transaction.savepoint_rollback(save_id)
                            <span class="hljs-keyword">raise</span> serializers.ValidationError(<span class="hljs-string">&apos;&#x5546;&#x54C1;&#x5E93;&#x5B58;&#x4E0D;&#x8DB3;&apos;</span>)

                        <span class="hljs-comment"># &#x7528;&#x4E8E;&#x6F14;&#x793A;&#x5E76;&#x53D1;&#x4E0B;&#x5355;</span>
                        <span class="hljs-comment"># import time</span>
                        <span class="hljs-comment"># time.sleep(5)</span>

                        <span class="hljs-comment"># &#x51CF;&#x5C11;&#x5E93;&#x5B58;</span>
                        <span class="hljs-comment"># sku.stock -= sku_count</span>
                        <span class="hljs-comment"># sku.sales += sku_count</span>
                        <span class="hljs-comment"># sku.save()</span>
                        new_stock = origin_stock - sku_count
                        new_sales = origin_sales + sku_count

                        <span class="hljs-comment"># &#x6839;&#x636E;&#x539F;&#x59CB;&#x5E93;&#x5B58;&#x6761;&#x4EF6;&#x66F4;&#x65B0;&#xFF0C;&#x8FD4;&#x56DE;&#x66F4;&#x65B0;&#x7684;&#x6761;&#x76EE;&#x6570;&#xFF0C;&#x4E50;&#x89C2;&#x9501;</span>
                        ret = SKU.objects.filter(id=sku.id, stock=origin_stock).update(stock=new_stock, sales=new_sales)
                        <span class="hljs-keyword">if</span> ret == <span class="hljs-number">0</span>:
                            <span class="hljs-keyword">continue</span>

                        <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x5546;&#x54C1;&#x7684;SPU &#x9500;&#x91CF;&#x4FE1;&#x606F;</span>
                        sku.goods.sales += sku_count
                        sku.goods.save()

                        <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x8BA2;&#x5355;&#x57FA;&#x672C;&#x4FE1;&#x606F;&#x7684;&#x6570;&#x636E;</span>
                        order.total_count += sku_count  <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x603B;&#x91D1;&#x989D;</span>
                        order.total_amount += (sku.price * sku_count)  <span class="hljs-comment"># &#x7D2F;&#x8BA1;&#x603B;&#x989D;</span>

                        <span class="hljs-comment"># &#x4FDD;&#x5B58;&#x8BA2;&#x5355;&#x5546;&#x54C1;</span>
                        OrderGoods.objects.create(
                            order=order,
                            sku=sku,
                            count=sku_count,
                            price=sku.price,
                        )

                        <span class="hljs-comment"># &#x66F4;&#x65B0;&#x6210;&#x529F;</span>
                        <span class="hljs-keyword">break</span>

                <span class="hljs-comment"># &#x66F4;&#x65B0;&#x8BA2;&#x5355;&#x7684;&#x91D1;&#x989D;&#x6570;&#x91CF;&#x4FE1;&#x606F;</span>
                order.total_amount += order.freight
                order.save()

            <span class="hljs-keyword">except</span> serializers.ValidationError:
                <span class="hljs-keyword">raise</span>
            <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
                logger.error(e)
                transaction.savepoint_rollback(save_id)
                <span class="hljs-keyword">raise</span>

            <span class="hljs-comment"># &#x66F4;&#x65B0;redis&#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x8D2D;&#x7269;&#x8F66;&#x6570;&#x636E;</span>
            pl = redis_conn.pipeline()
            pl.hdel(<span class="hljs-string">&apos;cart_%s&apos;</span> % user.id, *cart_selected)
            pl.srem(<span class="hljs-string">&apos;cart_selected_%s&apos;</span> % user.id, *cart_selected)
            pl.execute()
            <span class="hljs-keyword">return</span> order
</code></pre>
<h3 id="5-&#x9700;&#x8981;&#x4FEE;&#x6539;mysql&#x7684;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;">5. &#x9700;&#x8981;&#x4FEE;&#x6539;MySQL&#x7684;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;</h3>
<p>&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;&#x6307;&#x7684;&#x662F;&#x5728;&#x5904;&#x7406;&#x540C;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7684;&#x591A;&#x4E2A;&#x4E8B;&#x52A1;&#x4E2D;&#xFF0C;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4FEE;&#x6539;&#x6570;&#x636E;&#x540E;&#xFF0C;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x4F55;&#x65F6;&#x80FD;&#x770B;&#x5230;&#x4FEE;&#x6539;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p>MySQL&#x6570;&#x636E;&#x5E93;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;&#x4E3B;&#x8981;&#x6709;&#x56DB;&#x79CD;&#xFF1A;</p>
<ul>
<li><strong>Serializable</strong>    &#x4E32;&#x884C;&#x5316;&#xFF0C;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x7684;&#x6267;&#x884C;</li>
<li><strong>Repeatable read</strong>  &#x53EF;&#x91CD;&#x590D;&#x8BFB;&#xFF0C;&#x65E0;&#x8BBA;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x662F;&#x5426;&#x4FEE;&#x6539;&#x5E76;&#x63D0;&#x4EA4;&#x4E86;&#x6570;&#x636E;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x4E8B;&#x52A1;&#x4E2D;&#x770B;&#x5230;&#x7684;&#x6570;&#x636E;&#x503C;&#x59CB;&#x7EC8;&#x4E0D;&#x53D7;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x5F71;&#x54CD;</li>
<li><strong>Read committed</strong>   &#x8BFB;&#x53D6;&#x5DF2;&#x63D0;&#x4EA4;&#xFF0C;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x63D0;&#x4EA4;&#x4E86;&#x5BF9;&#x6570;&#x636E;&#x7684;&#x4FEE;&#x6539;&#x540E;&#xFF0C;&#x672C;&#x4E8B;&#x52A1;&#x5C31;&#x80FD;&#x8BFB;&#x53D6;&#x5230;&#x4FEE;&#x6539;&#x540E;&#x7684;&#x6570;&#x636E;&#x503C;</li>
<li><strong>Read uncommitted</strong>  &#x8BFB;&#x53D6;&#x4E3A;&#x63D0;&#x4EA4;&#xFF0C;&#x5176;&#x4ED6;&#x4E8B;&#x52A1;&#x53EA;&#x8981;&#x4FEE;&#x6539;&#x4E86;&#x6570;&#x636E;&#xFF0C;&#x5373;&#x4F7F;&#x672A;&#x63D0;&#x4EA4;&#xFF0C;&#x672C;&#x4E8B;&#x52A1;&#x4E5F;&#x80FD;&#x770B;&#x5230;&#x4FEE;&#x6539;&#x540E;&#x7684;&#x6570;&#x636E;&#x503C;&#x3002;</li>
</ul>
<p>MySQL&#x6570;&#x636E;&#x5E93;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;&#x53EF;&#x91CD;&#x590D;&#x8BFB;&#xFF08; Repeatable read&#xFF09;&#xFF0C;&#x800C;&#x4F7F;&#x7528;&#x4E50;&#x89C2;&#x9501;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x4E8B;&#x52A1;&#x4FEE;&#x6539;&#x4E86;&#x5E93;&#x5B58;&#x5E76;&#x63D0;&#x4EA4;&#x4E86;&#x4E8B;&#x52A1;&#xFF0C;&#x90A3;&#x5176;&#x4ED6;&#x7684;&#x4E8B;&#x52A1;&#x5E94;&#x8BE5;&#x53EF;&#x4EE5;&#x8BFB;&#x53D6;&#x5230;&#x4FEE;&#x6539;&#x540E;&#x7684;&#x6570;&#x636E;&#x503C;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x53EF;&#x91CD;&#x590D;&#x8BFB;&#x7684;&#x9694;&#x79BB;&#x7EA7;&#x522B;&#xFF0C;&#x5E94;&#x8BE5;&#x4FEE;&#x6539;&#x4E3A;&#x8BFB;&#x53D6;&#x5DF2;&#x63D0;&#x4EA4;Read committed&#x3002;</p>
<p>&#x4FEE;&#x6539;&#x65B9;&#x6CD5;&#xFF1A;</p>
<p><img src="../images/&#x7F16;&#x8F91;&#x6570;&#x636E;&#x5E93;&#x914D;&#x7F6E;&#x6587;&#x4EF6;.jpeg" alt="&#x6253;&#x5F00;&#x914D;&#x7F6E;&#x6587;&#x4EF6;"></p>
<p><img src="../images/&#x4FEE;&#x6539;mysql&#x7684;&#x4E8B;&#x52A1;&#x9694;&#x79BB;&#x7EA7;&#x522B;.jpeg" alt="&#x4FEE;&#x6539;&#x9694;&#x79BB;&#x7EA7;&#x522B;"></p>
<h3 id="6-&#x524D;&#x7AEF;&#x5B9E;&#x73B0;">6. &#x524D;&#x7AEF;&#x5B9E;&#x73B0;</h3>
<p>&#x4FEE;&#x6539;place_order.js&#x6587;&#x4EF6;</p>
<pre><code class="lang-js">        <span class="hljs-comment">// &#x63D0;&#x4EA4;&#x8BA2;&#x5355;</span>
        on_order_submit: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.order_submitting == <span class="hljs-literal">false</span>){
                <span class="hljs-keyword">this</span>.order_submitting = <span class="hljs-literal">true</span>;
                axios.post(<span class="hljs-keyword">this</span>.host+<span class="hljs-string">&apos;/orders/&apos;</span>, {
                        address: <span class="hljs-keyword">this</span>.nowsite,
                        pay_method: <span class="hljs-keyword">this</span>.pay_method
                    }, {
                        headers: {
                            <span class="hljs-string">&apos;Authorization&apos;</span>: <span class="hljs-string">&apos;JWT &apos;</span> + <span class="hljs-keyword">this</span>.token
                        },
                        responseType: <span class="hljs-string">&apos;json&apos;</span>
                    })
                    .then(response =&gt; {
                        location.href = <span class="hljs-string">&apos;/order_success.html?order_id=&apos;</span>+response.data.order_id
                            +<span class="hljs-string">&apos;&amp;amount=&apos;</span>+<span class="hljs-keyword">this</span>.payment_amount
                            +<span class="hljs-string">&apos;&amp;pay=&apos;</span>+<span class="hljs-keyword">this</span>.pay_method;
                    })
                    .catch(error =&gt; {
                        <span class="hljs-keyword">this</span>.order_submitting = <span class="hljs-literal">false</span>;
                        alert(error.response.data[<span class="hljs-number">0</span>]);
                    })
            }
        }
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C05-Order/PlaceOrder.html" class="navigation navigation-prev " aria-label="Previous page: 订单结算"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Order/OrderSuccess.html" class="navigation navigation-next " aria-label="Next page: 下单成功"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
